<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">

		<u-form :model="info" ref="dataForm" :errorType="['toast']" label-position="left" label-width="150"
			label-align="left">
			<u-form-item label="请购单别" v-if="judgeShow('ta001')">
				<u-input v-model="info.ta001" placeholder="请购单别" disabled></u-input>
			</u-form-item>
			<u-form-item label="请购单号" v-if="judgeShow('ta002')">
				<u-input v-model="info.ta002" placeholder="请购单号" disabled></u-input>
			</u-form-item>
			<u-form-item label="请购人员" v-if="judgeShow('ta012')">
				<u-input v-model="buyName" placeholder="请购人员" disabled></u-input>
			</u-form-item>
			<u-form-item label="请购日期" v-if="judgeShow('ta003')">
				<u-input v-model="info.ta003" placeholder="请购日期" disabled></u-input>
			</u-form-item>
			<u-form-item label="流程请购部门" v-if="judgeShow('udf02')">
				<u-input v-model="info.udf02" placeholder="流程请购部门" disabled></u-input>
			</u-form-item>


			<u-form-item label="订单备注" v-if="judgeShow('ta006')">
				<u-input v-model="info.ta006" type="textarea" placeholder="请输入订单备注" disabled></u-input>
			</u-form-item>
			<u-form-item label="相关附件" v-if="judgeShow('attachmentUpload')">
				<jnpf-file v-model="info.attachmentUpload" disabled />
			</u-form-item>

			<view class="jnpf-table" style="margin-top: 24rpx;">
				<view class="jnpf-table-item" style="margin-bottom: 24rpx;" v-for="(item,i) in lst" :key="i">
					<u-form-item label="序号">
						{{i+1}}
					</u-form-item>
					<u-form-item label="品号" v-if="judgeShow('tb004')">
						<u-input v-model="item.tb004" placeholder="品号" disabled></u-input>
					</u-form-item>
					<u-form-item label="品名" v-if="judgeShow('tb005')">
						<u-input v-model="item.tb005" placeholder="品名" disabled></u-input>
					</u-form-item>
					<u-form-item label="规格" v-if="judgeShow('tb006')">
						<u-input v-model="item.tb006" placeholder="规格" disabled></u-input>
					</u-form-item>
					<u-form-item label="单位" v-if="judgeShow('tb007')">
						<u-input v-model="item.tb007" placeholder="单位" disabled></u-input>
					</u-form-item>
					<u-form-item label="数量" v-if="judgeShow('tb014')">
						<u-input v-model="item.tb014" placeholder="数量" disabled></u-input>
					</u-form-item>
					<u-form-item label="需求日期" v-if="judgeShow('tb011')">
						<u-input v-model="item.tb011" placeholder="需求日期" disabled></u-input>
					</u-form-item>
					<u-form-item label="备注" v-if="judgeShow('tb012')" disabled>
						<u-input v-model="item.tb012" type="textarea" placeholder="请输入订单备注" disabled></u-input>
					</u-form-item>
				</view>
				<u-form-item label="合计" style="margin-top: 24rpx;" v-if="judgeShow('ta011')">
					{{info.ta011||0}}
				</u-form-item>
			</view>
		</u-form>
	</view>
</template>

<script>
	import {
		getFlowInfo
	} from '@/api/workFlow/flowEngine'
	import {
		getDataInterfaceDataInfo
	} from '@/api/common'
	import comMixin from '../mixin'
	import {
		getGoodsList,
		getCustomerList,
		Info,
		Create,
		Update
	} from '@/api/apply/order'
	export default {
		name: 'crmOrder',
		mixins: [comMixin],
		data() {
			return {
				hide: false,
				billEnCode: 'OrderNumber',
				dataForm: {

				},
				lst: [],
				info: {},
				formConf: {},
				id: '',
				buyName: '',
				buyTwoName: '',
			}
		},

		methods: {
			init(data, id) {
				this.setting = data
				this.id = id
				this.getInfo(data)
			},
			getInfo(data) {
				getFlowInfo(data.id).then(res => {
					if (res.code == 200) {
						this.info = res.data
						this.lst = res.data.purtbList
						this.getDataInterfaceDataInfoMV002(this.info.ta012)
						this.getDataInterfaceDataInfoMV003(this.info.udf02)
					}
				})
			},
			getDataInterfaceDataInfoMV002(id) {
				let data = {
					id,
					interfaceId: '445425382452126213',
					dblink: this.id,
					propsValue: 'MV001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('445425382452126213', data).then(res => {
					if (res.code == 200) {
						this.buyName = res.data.JoinStr
					}
				})
			},
			getDataInterfaceDataInfoMV003(id) {
				let data = {
					id,
					interfaceId: '381240499823979141',
					dblink: this.id,
					propsValue: 'ME001',
					relationField: 'ME002',
				}
				getDataInterfaceDataInfo('381240499823979141', data).then(res => {
					if (res.code == 200) {
						this.buyTwoName = res.data.ME002
					}
				})
			},
			selfGetInfo() {
				Info(this.setting.id).then(res => {
					this.dataForm = res.data
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson)
					}
				})
			},
			selfSubmit() {
				this.dataForm.status = this.eventType === 'submit' ? 0 : 1
				if (this.eventType === 'save') return this.selfHandleRequest()
				uni.showModal({
					title: '提示',
					content: '您确定要提交当前流程吗？',
					success: res => {
						if (res.confirm) {
							this.selfHandleRequest()
						}
					}
				})
			},
			selfHandleRequest() {
				if (!this.dataForm.id) delete(this.dataForm.id)
				const formMethod = this.dataForm.id ? Update : Create
				formMethod(this.dataForm).then(res => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit('refresh')
								uni.navigateBack()
							}, 1500)
						}
					})
				})
			},
			initData() {
				getGoodsList().then(res => {
					this.goodsOptions = res.data.list
				})
				getCustomerList().then(res => {
					this.customerOptions = res.data.list
				})
			},
			selfInit() {
				this.addGoods()
				this.addPlan()
			},
			onChange(e) {
				const user = e[0]
				this.dataForm.salesmanName = user.fullName
			},
			addGoods() {
				const item = {
					goodsId: '',
					goodsCode: '',
					goodsName: '',
					specifications: '',
					unit: '',
					qty: '',
					price: '',
					amount: '',
					discount: null,
					cess: null,
					actualPrice: '',
					actualAmount: '',
					description: ''
				}
				this.dataForm.goodsList.push(item)
			},
			delGoods(index) {
				this.dataForm.goodsList.splice(index, 1)
			},
			addPlan() {
				const item = {
					receivableDate: '',
					receivableRate: '',
					receivableMoney: '',
					receivableMode: '',
					abstract: ''
				}
				this.dataForm.collectionPlanList.push(item)
			},
			delPlan(index) {
				this.dataForm.collectionPlanList.splice(index, 1)
			},
			onGoodsChange(e, i) {
				const goods = e[0]
				this.dataForm.goodsList[i] = {
					goodsId: goods.id,
					goodsCode: goods.code,
					goodsName: goods.text,
					specifications: goods.specifications,
					unit: goods.unit,
					qty: 1,
					price: goods.price,
					amount: goods.price,
					discount: 100,
					cess: 0,
					actualPrice: goods.price,
					actualAmount: goods.price,
					description: ''
				}
				this.$forceUpdate()
			},
			onCustomerChange(e) {
				const customer = e[0]
				this.dataForm.customerName = customer.text
			},
			count(row) {
				//金额 = 数量*单价
				row.amount = this.jnpf.toDecimal(parseFloat(row.price) * parseFloat(row.qty))
				//折扣价 = (单价*折扣)
				var discountPrice = row.price * (row.discount / 100);
				//实际单价 = 折扣价 * (1 + (税率 / 100))
				row.actualPrice = this.jnpf.toDecimal(discountPrice * (1 + (row.cess / 100)));
				//实际金额
				row.actualAmount = this.jnpf.toDecimal(parseFloat(row.actualPrice) * parseFloat(row
					.qty))
				this.$forceUpdate()
			}
		}
	}
</script>